package com.chuanye.jiguangimdemo.adapters;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.chuanye.jiguangimdemo.R;

import java.util.List;

import cn.jpush.im.android.api.enums.ConversationType;
import cn.jpush.im.android.api.model.Conversation;
import cn.jpush.im.android.api.model.UserInfo;

public class ForwardMsgAdapter extends BaseAdapter {

    private Context mContext;
    private List<Conversation> mConvList;

    public ForwardMsgAdapter(Context mContext, List<Conversation> mConvList) {
        this.mContext = mContext;
        this.mConvList = mConvList;
    }

    @Override
    public int getCount() {
        return mConvList.size();
    }

    @Override
    public Object getItem(int position) {
        return mConvList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null){
            holder = new ViewHolder();
            convertView = View.inflate(mContext, R.layout.item_contact,null);
            holder.name = convertView.findViewById(R.id.name);
            holder.avatar = convertView.findViewById(R.id.head_icon_iv);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }

        Conversation conversation = mConvList.get(position);
        if (conversation.getType() == ConversationType.group) {//群组
            holder.name.setText(conversation.getTitle());
            holder.avatar.setImageResource(R.drawable.group);
        } else {
            UserInfo userInfo = (UserInfo) conversation.getTargetInfo();
            holder.name.setText(userInfo.getDisplayName());
            if (userInfo.getAvatarFile() != null) {
                holder.avatar.setImageBitmap(BitmapFactory.decodeFile(userInfo.getAvatarFile().getAbsolutePath()));
            }else {
                holder.avatar.setImageResource(R.drawable.rc_default_portrait);
            }
        }
        return convertView;
    }


    private static class ViewHolder{
        TextView name;
        ImageView avatar;
    }
}
